package chapter07

/**
 * 集合计算高级函数
 */
object Test14_HighLevelFunction_Map {
  def main(args: Array[String]): Unit = {
    var list = List(1, 2, 3, 4, 5, 6, 7, 8, 9)

    //1、过滤
    //选取偶数
    val evenList = list.filter((elem: Int) => {
      elem % 2 == 0
    })
    println(evenList)

    //选取奇数
    println(list.filter(_ % 2 == 1))


    //2、map
    //把集合中每个数乘以2
    println("---------------------------")
    println(list.map(_ * 2))
    println(list.map(x => x * x))


    //3、扁平化
    println("扁平化")
    val nestedList: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9))
    val ints = nestedList(0) ::: nestedList(1) ::: nestedList(2)
    println(ints)

    val flatten = nestedList.flatten
    println(flatten)
    println("============================")


    //4、扁平映射
    println("扁平映射")
    //将一组字符串进行分词，并保存成单词的列表
    val strings: List[String] = List("hello word", "hello scala", "hello java", "we study")
    val stringses: List[Array[String]] = strings.map(_.split(" ")) //分词
    val flattenList = stringses.flatten
    println(flattenList)


    val strings1 = strings.flatMap(_.split(" "))
    println(strings1)

    //5、分组groupBy
    //分成奇偶两组
    val groupMap: Map[Int, List[Int]] = list.groupBy(_ % 2)
    val groupMap2: Map[String, List[Int]] = list.groupBy(data => {
      if (data % 2 == 0) "偶数" else "奇数"
    })

    println(groupMap)
    println(groupMap2)


    //6、给定一组词汇，按照单词的首字母进行分组
    val strings2 = List("china", "america", "alice", "canada", "cary", "bob", "japan")
    println(strings2.groupBy(_.charAt(0)))
  }
}
